package com.chenjt.jvm.first.five_heap;

/**
 * 注释：标量替换
 *
 * +DoEscapeAnalysis：开启逃逸分析
 * -EliminateAllocations：关闭标量替换
 * -Xmx100m -Xms100m -XX:+DoEscapeAnalysis -XX:+PrintGC -XX:-EliminateAllocations
 *
 * +EliminateAllocations：开启标量替换
 * -Xmx100m -Xms100m -XX:+DoEscapeAnalysis -XX:+PrintGC -XX:+EliminateAllocations
 *
 * 以上两种情况做对比（开启/关闭标量替换）
 *
 * @author chenjt
 * @date 2025/6/10 10:16
 */
public class ScalarReplace {

    public static class User{
        public int id;
        public String name;
    }

    public static void alloc(){
        User u = new User();// 未发生逃逸，就可以把该对象替换成标量
        u.id = 5;
        u.name = "chenjt";
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            alloc();
        }
        long end = System.currentTimeMillis();
        System.out.println("花费的时间为：" + (end - start) + "ms");
    }


}
